package com.wsj.aop.customize;

import com.wsj.services.ProxyTest;
import com.wsj.services.impl.ProxyTestImpl;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.lang.Nullable;

/**
 * description:对目标类进行自定义前置增强
 *
 * @author yang
 * @date 2019/2/21 16:55
 */
public class CustomizeMethodAdvice implements MethodBeforeAdvice {

  @Override
  public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
    System.out.println("前置增强方法：" + method.getName());
    if (args.length > 0) {
      for (Object arg : args) {
        System.out.println("参数：" + arg);
      }
    }
    System.out.println("目标类：" + target);
  }


  public static void main(String[] args) {
    CustomizeMethodAdvice methodAdvice = new CustomizeMethodAdvice();
    CustomizeStaticPointcutAdvisor advisor = new CustomizeStaticPointcutAdvisor();
    advisor.setAdvice(methodAdvice);
    ProxyTestImpl proxyTarget = new ProxyTestImpl();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(proxyTarget);
    //proxyFactory.addAdvice(methodAdvice);
    proxyFactory.addAdvisor(advisor);

    ProxyTest proxy = (ProxyTest) proxyFactory.getProxy();
    proxy.print();
  }
}
